//
//  BindingPhoneViewController.m
//  MagazineShangejie
//
//  Created by mac on 15/1/19.
//  Copyright (c) 2015年 CnbizMedia. All rights reserved.
//

#import "BindingPhoneViewController.h"
#import "Util.h"
@interface BindingPhoneViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *Verification;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *VerificationButton;//验证
@property (weak, nonatomic) IBOutlet UIButton *next;//下一步、注册

@end

@implementation BindingPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=UIColorFromRGB(0xf5f5f5);
    
    [self.VerificationButton addTarget:self action:@selector(VerificationAction) forControlEvents:UIControlEventTouchUpInside];
    [self.VerificationButton setTag:0];
    
    self.title=@"绑定手机";
    [self.next setTitle:@"确定绑定" forState:UIControlStateNormal];
    [self.next addTarget:self action:@selector(registered) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)VerificationAction{
    if (![CommonHelper isValidatePhone:self.phone.text]) {
        [SVProgressHUD showErrorWithStatus:@"您输入的号码有误!"];
        return;
    }
    [[AppAPIClient sharedClient] GET:@"User/VerifyMobile" parameters:@{@"mobile":self.phone.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary*dic=[NSMutableDictionary dictionaryWithDictionary:IFUserLogin];
        
        [dic setObject:self.phone.text forKey:@"mobile"];
        [UserDefaults setObject:dic forKey:@"user"];
        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    [self.VerificationButton setTag:30];
    [self VerificationA];
    [self.VerificationButton setBackgroundColor:[UIColor lightGrayColor]];
    
}
-(void)VerificationA{
    [self.VerificationButton setEnabled:YES];
    if (self.VerificationButton.tag==0) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(VerificationA) object:nil];
        [self.VerificationButton setTitle:@"重新获取" forState:UIControlStateNormal];

        return;
    }
    
    self.VerificationButton.tag-=1;
    [self.VerificationButton setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.VerificationButton.tag] forState:UIControlStateNormal];
    self.VerificationButton.titleLabel.text=[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.VerificationButton.tag];
    [self performSelector:@selector(VerificationA) withObject:nil afterDelay:1];
    [self.VerificationButton setEnabled:NO];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resignFirst];
}
-(void)resignFirst{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}


-(void)registered{

    [self resignFirst];
    if([CommonHelper isValidatePhone:self.phone.text]&&self.Verification.text){
        [SVProgressHUD show];
        [[AppAPIClient sharedClient] GET:@"User/VerifyMobile" parameters:@{@"mobile":self.phone.text,@"code":self.Verification.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
            int code = [responseObject[@"code"] intValue];
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
            if(code == 111){
//                [alert setMessage:@"用户名已存在。"];
            }else if(code == 100){
//                [alert setMessage:@"用户信息不完整。"];
            }else if(code == 112){
//                [alert setMessage:@"用户邮箱已存在。"];
            }else if(code == 1){
//                [alert setMessage:@"注册成功！"];
                [SVProgressHUD showSuccessWithStatus:@"绑定成功"];
//                [self.navigationController popViewControllerAnimated:YES];
                [self popOfDismss];
            }else{
                [SVProgressHUD showErrorWithStatus:@"验证码错误"];
            }

            NSLog(@"%@",responseObject);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"验证码错误"];
            NSLog(@"Error:%@ => %@",error,operation.responseString);
        }];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户信息无效，请检查信息！" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [alert show];
    }
}


-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        //        self.hidesBottomBarWhenPushed = NO;
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        //    self.hidesBottomBarWhenPushed = NO;
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end
